<template>
    <div class='login'>
        <el-form style="margin-left: 600px;margin-top: 200px" :model="UserForm" status-icon :rules="rules" ref="UserForm" label-width="100px" class="demo-ruleForm">
            <el-form-item label="账号" prop="userId">
                <el-input type="text" v-model.number="UserForm.userId" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="密码" prop="userPassword">
                <el-input type="password" v-model="UserForm.userPassword" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" @click="submitForm('UserForm')">登录</el-button>
            </el-form-item>
        </el-form>
    </div>
</template>
<script>
    export default {
        name: 'Login',
        data() {
            var validatePass = (rule, value, callback) => {
                if (value === '') {
                    callback(new Error('请输入密码'));
                }else{
                    callback();
                }
            };
            var checkUsername = (rule, value, callback) => {
                if (value === '') {
                    callback(new Error('请输入账号'));
                }else{
                    callback();
                }
            };
            return {
                UserForm: {
                    userPassword: '',
                    userId: ''
                },
                rules: {
                    userPassword: [
                        { validator: validatePass, trigger: 'blur' }
                    ],
                    userId: [
                        { validator: checkUsername, trigger: 'blur' }
                    ]
                }
            };
        },
        methods: {
            submitForm(formName) {
                this.$refs[formName].validate((valid) => {
                    if (valid) {
                        this.Login();
                    } else {
                        console.log('error submit!!');
                        return false;
                    }
                });
            },
            Login(){
                this.axios.post('http://120.26.99.230:8092/user/login', this.UserForm
                ).then(res =>
                {

                    console.log(res)
                    if(res.data.is == true)
                    {
                        const token = res.data.data.token
                        localStorage.setItem("token",token)
                        this.$router.push('/Home/main')
                    }
                    else
                    {
                        this.$message.error(res.data.msg);
                    }
                })
            }
        },
    }
</script>
<style>
    .login{
        text-align: center;
    }
    .el-form-item{
        width: 500px;
    }
</style>